package com.reggie.controller.admin;

import com.reggie.dto.DishDTO;
import com.reggie.dto.DishPageQueryDTO;
import com.reggie.entity.Dish;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.DishService;
import com.reggie.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关的接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询接口")
    public R<PageResult> page(DishPageQueryDTO pageQueryDTO) {
        log.info("菜品分类查询：" + pageQueryDTO);
        PageResult pageResult = dishService.pageQuery(pageQueryDTO);
        return R.success(pageResult);
    }

    /**
     * 菜品起售、停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售、停售")
    public R<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("菜品起售停售参数：{},{}" + status, id);
        dishService.startOrStop(status, id);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.success();
    }

    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品接口")
    public R<List<Dish>> findBuId(Long categoryId) {
        log.info("根据分类id查询的参数：" + categoryId);
        List<Dish> dishs = dishService.findCategoryById(categoryId);
        return R.success(dishs);
    }

    /**
     * 新增菜品
     */
    @PostMapping
    @ApiOperation("新增菜品接口")
    public R<String> save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：" + dishDTO);
        dishService.save(dishDTO);
        cleanCache("dish_" + dishDTO.getCategoryId());
        return R.success();
    }

    /**
     * 修改菜品
     */
    @PutMapping
    @ApiOperation("修改菜品接口")
    public R<String> update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品参数：" + dishDTO);
        dishService.update(dishDTO);
        cleanCache("dish_*");
        return R.success();
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public R<DishVO> findById(@PathVariable Long id){
        log.info("根据id查询菜品参数：" + id);
        DishVO dishVO = dishService.findById(id);
        return R.success(dishVO);
    }

    /**
     * 批量删除菜品
     */
    @DeleteMapping
    @ApiOperation("批量删除接口")
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("批量删除的参数：" + ids);
        dishService.delete(ids);
        cleanCache("dish_*");
        return R.success();
    }

    /**
     * 清理缓存的方法
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
